package com.teamall.core.common;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author xuqw
 *
 */
public class JModelAndView extends ModelAndView {
	/**
	 * 构造函数1
	 * @param viewName
	 */
	public JModelAndView(String viewName){
		HttpServletRequest request = getRequest();
		String webPath = request.getContextPath();
		if("/".equals(webPath)){
			webPath="";
		}
		super.setViewName(viewName);
		super.addObject("webPath", webPath);
	}
	
	/**
	 * 构造函数2
	 * @param viewName 跳转的url
	 * @param key 其中一个参数的key
	 * @param value 其中一个参数的value
	 * @param request 
	 */
	public JModelAndView(String viewName,String key,String value){
		HttpServletRequest request = this.getRequest();
		String webPath = request.getContextPath();
		if("/".equals(webPath)){
			webPath="";
		}
		super.setViewName(viewName);
		super.addObject(key, value);
		super.addObject("webPath", webPath);
	}
	
	
	/**
	 * 构造函数3-后面废弃，由构造函数1替代
	 * @param viewName
	 * @param request
	 */
	public JModelAndView(String viewName,HttpServletRequest request){
		String webPath = request.getContextPath();
		if("/".equals(webPath)){
			webPath="";
		}
		super.setViewName(viewName);
		super.addObject("webPath", webPath);
    	
	}
	
	/**
	 * 构造函数4后面废弃，由构造函数2替代
	 * @param viewName 跳转的url
	 * @param key 其中一个参数的key
	 * @param value 其中一个参数的value
	 * @param request 
	 */
	public JModelAndView(String viewName,String key,String value,HttpServletRequest request){
		String webPath = request.getContextPath();
		if("/".equals(webPath)){
			webPath="";
		}
		super.setViewName(viewName);
		super.addObject(key, value);
		super.addObject("webPath", webPath);
	}
	
	
	/**
	 * 获取request对象
	 * @return
	 */
	public HttpServletRequest getRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
}
